//
//  ActivityViewController.m
//  XiangCaoJiaYuan
//
//  Created by long Kan on 17/10/8.
//  Copyright © 2017年 tongj. All rights reserved.
//

#import "ActivityViewController.h"
#import "ActivityModel.h"
#import "ActivityCell.h"
#import "ActivityDetailController.h"
@interface ActivityViewController ()<UITableViewDelegate,UITableViewDataSource,MJRefreshBaseViewDelegate>
{
    UITableView *_tableView;
    MJRefreshHeaderView *_headerView;
    MJRefreshFooterView *_footerView;
    BOOL _isLoad;//是否处于网络请求状态
    NSInteger _pageIndex;
    
}
@property (nonatomic,strong)NSMutableArray *dataArray1;
@end

@implementation ActivityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArray1  = [[NSMutableArray alloc]init];
    
    self.title  = @"我的活动";
    UIButton *backbutton = [[UIButton alloc]initWithFrame:CGRectMake(10, 20, 15, 25)];
    [backbutton setBackgroundImage:[UIImage imageNamed:@"香草园地-发布信息_03@2x"] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(goBacktoUp) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:backbutton];
    self.navigationItem.leftBarButtonItem = backItem;
    [self questData];
    
    [self cerateTableView];
}
-(void)goBacktoUp
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""]forBarMetrics:UIBarMetricsDefault];
}
-(void)cerateTableView
{
    _pageIndex = 0;
    _isLoad =NO;
    
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UIScreen_WIDTH, UIScreen_HEIGHT-50)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    _headerView=[MJRefreshHeaderView header];
    _headerView.scrollView=_tableView;
    _headerView.delegate=self;
    //上拉加载更多的视图
    _footerView=[MJRefreshFooterView footer];
    _footerView.scrollView=_tableView;
    _footerView.delegate=self;
    
}
-(void)questData{
    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
    NSString *uid = [defaults objectForKey:@"shop_guid"];
    NSString *url = [NSString stringWithFormat:@"%@campaign/infos?shop_guid=%@&times=%ld",ServiceURL2,uid,_pageIndex];
    [YBRequestManager GET:url finished:^(NSData *data) {
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *array = [NSMutableArray array];
        NSArray *str11 = dict[@"data"];
        
        for (NSDictionary *dict1 in str11)
        {
            [array addObject:dict1];
        }
        if (_pageIndex==0) {
            [_dataArray1 removeAllObjects];
        }
        NSArray *array2 = [ActivityModel arrayOfModelsFromDictionaries:array error:nil];
        [_dataArray1 addObjectsFromArray:array2];
        [_tableView reloadData];
        _isLoad = NO;
        [_headerView endRefreshing];
        [_footerView endRefreshing];
        
    } failed:^(NSString *error) {
        
    }];
    
}
-(void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    if (_isLoad) {
        return;
    }
    _isLoad = YES;
    //如果是下拉刷新
    
    
    if (refreshView==_headerView) {
        _pageIndex=0;
    }else if (refreshView==_footerView){
        _pageIndex++;
    }
    [self questData];
    
}
- (void)dealloc {
    [_headerView free];
    [_footerView free];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];// 取消选中
    ActivityModel *model = _dataArray1[indexPath.row];
    ActivityDetailController *detail = [[ActivityDetailController alloc]init];
    detail.campaign_guid = model.campaign_guid;
    [self.navigationController pushViewController:detail animated:YES];
    
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *identifier = @"ActivityCell";
    
    ActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell ==nil) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"ActivityCell" owner:nil options:nil][0];
    }
    ActivityModel *model = _dataArray1[indexPath.row];
    [cell setModel:model];
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray1.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}

@end
